//#define PC

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

#if PC
using System.Diagnostics;
#endif

namespace TimeWar
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class GameCursor : ITimeWarObject
    {

        private Vector2 position;
        private Texture2D tardisCursor, drCursor, dalekCursor;
        private bool isOverTimeLords = false, isOverDaleks = false;
        private SpriteBatch batch;

        public GameCursor(SpriteBatch spriteBatch)
        {
            // TODO: Construct any child components here
            position = new Vector2();

#if PC
            position.X = Mouse.GetState().X;
            position.Y = Mouse.GetState().Y;
#endif
            batch = spriteBatch;
            Initialize();

        }

        public void setSpriteBatch(SpriteBatch pSpriteBatch)
        {
            batch = pSpriteBatch;
        }

        public bool IsOverTimeLords
        {
            get
            {
                return isOverTimeLords;
            }
            set
            {
                isOverTimeLords = value;
            }
        }

        public bool IsOverDaleks
        {
            get
            {
                return isOverDaleks;
            }
            set
            {
                isOverDaleks = value;
            }
        }

        public Vector2 Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
            }
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public void Initialize()
        {
            // TODO: Add your initialization code here
            tardisCursor = CM.loadTexture(@"Textures\\tardis");
            drCursor = CM.loadTexture(@"Textures\\9thDr");
            dalekCursor = CM.loadTexture(@"Textures\\dalekSec");

            position.X = 600;
            position.Y = 350;

        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public GameState Update(TimeSpan gameTime, TimeSpan elapsedTime)
        {
            return GameState.None;

        }

        public void Render()
        {

            //default
#if PC
            position.X = Mouse.GetState().X;
            position.Y = Mouse.GetState().Y;
#endif
            float x = XInputHelper.GamePads[PlayerIndex.One].ThumbStickLeftX;
            float y = XInputHelper.GamePads[PlayerIndex.One].ThumbStickLeftY;
            position.X += Convert.ToInt32(x) * 4;
            position.Y -= Convert.ToInt32(y) * 4;

            if (position.X < 0)
            {
                position.X = 0;
            }
            else if (position.X > 16000)
            {
                position.X = 16000;
            }

            if (position.Y < 0)
            {
                position.Y = 0;
            }
            else if (position.Y > 9000)
            {
                position.Y = 9000;
            }


            if (isOverTimeLords == true && isOverDaleks == false)
            {
                batch.Draw(drCursor, new Rectangle(Convert.ToInt32(position.X), Convert.ToInt32(position.Y), drCursor.Width, drCursor.Height), null, Color.White, 0, new Vector2(0, 0), SpriteEffects.None, 0);
#if PC
                Trace.WriteLine("[" + DateTime.Now + "]: Cursor over Time Lords.");
#endif
                
            }
            else if (isOverDaleks == true && isOverTimeLords == false)
            {
                batch.Draw(dalekCursor, new Rectangle(Convert.ToInt32(position.X), Convert.ToInt32(position.Y), dalekCursor.Width, dalekCursor.Height), null, Color.White, 0, new Vector2(0, 0), SpriteEffects.None, 0);
#if PC
                Trace.WriteLine("[" + DateTime.Now + "]: Cursor over Daleks.");
#endif

            }
            else
            {
                //batch.Draw(tardisCursor, position, Color.White);
                batch.Draw(tardisCursor, new Rectangle(Convert.ToInt32(position.X), Convert.ToInt32(position.Y), tardisCursor.Width, tardisCursor.Height), null, Color.White, 0, new Vector2(0, 0), SpriteEffects.None, 0);

            }

        }


    }
}